
capture log close
log using $log/EDS_07_similarity, text replace

clear
clear matrix
clear mata
set mem 2g
set matsize 800
set more off
set maxvar 20000
set trace off
set varabbrev off

 
	 
#d;
		global labor_market "region_ba_aloq_zivall bip_real_pc region_ba_vacancies 
	   sector_manufacturing sector_service  mobirate_avg5 sector_agriculture
		"; 
		#d cr
		
		
		
	
	 use $data/data_pair, clear
		keep if year==2008
	
 	
		gen mobirate_avg5=exp(log_mobirate_avg5)
	
	  
		foreach var in $labor_market  {
		bys region year: egen `var'_m = mean(`var')
		drop `var'
		rename `var'_m `var'
		}
		
		bys region_pair region: keep if _n==1
		
		

		set seed 1224767
		gen x=runiform()
		
		bys region_pair: egen rk=rank(x)
		
			
			

		
		
		foreach var in $labor_market  {
		bys region_pair: gen `var'_high_temp = `var' if rk==2
		bys region_pair: gen `var'_low_temp = `var' if rk==1

		bys region_pair: egen `var'_high = max(`var'_high_temp)
		bys region_pair: egen `var'_low = min(`var'_low_temp)
		
		gen `var'_diff = abs(`var'_high - `var'_low)
		sum `var'_diff
		scalar m1_`var' = r(mean)
		drop `var'_high_temp `var'_low_temp `var'_high `var'_low `var'_diff
		}
		
		drop rk
		
		bys region: keep if _n==1
		
		forvalues k = 1/3 {
		local j = `k'-1
		preserve
		cap drop x
		if `k'==1 set seed 1224767
		if `k'==2 set seed 9892347
		if `k'==3 set seed 7552052
		
		gen x=runiform()
		sort x 
		gen pair_sim = _n + `j'*178

		tempfile pair_sim
		save `pair_sim'
		
		
		drop pair_sim x

		set seed 6526526
		gen x=runiform()
		sort x 
		gen pair_sim = _n + `j'*178
		append using `pair_sim'
		tempfile match`k'
		save `match`k''
		restore
		}
		
		use `match1', clear
		append using `match2'
		append using `match3'
		
		
		bys pair_sim: egen rk=rank(x)
		
		foreach var in $labor_market  {
		bys pair_sim: gen `var'_high_temp = `var' if rk==2
		bys pair_sim: gen `var'_low_temp = `var' if rk==1

		bys pair_sim: egen `var'_high = max(`var'_high_temp)
		bys pair_sim: egen `var'_low = min(`var'_low_temp)
		
		gen `var'_diff = abs(`var'_high - `var'_low)
		sum `var'_diff
		scalar m2_`var' = r(mean)
		drop `var'_high_temp `var'_low_temp `var'_high `var'_low `var'_diff
		
		
		
		matrix d_`var' = m2_`var',m1_`var'
		
	  }
		
		
		
		
				#d;
		matrix d=
		d_region_ba_aloq_zivall \
		d_bip_real_pc \
		d_region_ba_vacancies \
		d_sector_agriculture \
		d_sector_manufacturing \
		d_sector_service  \
		d_mobirate_avg5 
			;
		#d cr
		
		
		#d;
		matrix rownames d =
		"Unemployment rate" 
		"GDP per capita in 1000EUR"
		"Vacancy rate"
		"Share in agriculture"
		"Share in manufacturing"
		"Share in service"
		"Migration rate"
		
		;
		#d cr
		
		matrix colnames d =  "Simulated pair" "Actual pair"
		
		mat list d
		estout matrix(d, fmt(5)) using $data/similarity_border_pairs.tex, style(tex) replace
		
	
		log close
	 
	

